class Picture < ActiveRecord::Base
  has_many :tag_count
  has_many :round
  has_many :tag, :through => :tag_count

  def self.get_random_picture
    c = Picture.count
    pid = rand(c)
    picture = Picture.first(:offset =>pid)
    return picture
  end
  
  def get_offlimit_tag
    tags = self.tag.first(3)
    #tags = TagCount.find_by_picture_id(id,:order=>"count DESC",:limit=>3)
    return tags
  end

  def add_tag(tag)
    puts "addtag:"
    puts tag.id
    puts tag.name
    puts self.id
    puts self.url
    tagcount = TagCount.find(:first, :conditions=>"tag_id = '#{tag.id}' AND picture_id = '#{self.id}'")
    if tagcount == nil
      tagcount = TagCount.new
      tagcount.picture = self
      tagcount.tag = tag
      tagcount.count = 1
    else
      tagcount.count = tagcount.count+1
    end
    tagcount.save
    puts "tagcount saved.."
  end

end
